﻿Imports System.Data
Public Class Form_articulos

    Private Sub Form_articulos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setear_grid_global(grilla_articulos)
        Cargo_articulos()
    End Sub
   
    Function Cargo_articulos()
        grilla_articulos.DataSource = data_Articulos.DefaultView
    End Function
   
    Private Sub tx_busqueda_articulo_TextChanged(sender As Object, e As EventArgs) Handles tx_busqueda_articulo.TextChanged
        data_Articulos.DefaultView.RowFilter = "[DESCRIPCIO] LIKE '" & tx_busqueda_articulo.Text & "*'"
    End Sub

    Private Sub grilla_articulos_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles grilla_articulos.CellClick
        Try

            COD_ARTICU = grilla_articulos.Item(0, grilla_articulos.CurrentRow.Index).Value
            DESCRIPCIO = grilla_articulos.Item(1, grilla_articulos.CurrentRow.Index).Value
            COD_MEDIDA = grilla_articulos.Item(2, grilla_articulos.CurrentRow.Index).Value
            PRECIO = grilla_articulos.Item(3, grilla_articulos.CurrentRow.Index).Value
            If COD_ARTICU <> "" Then
                obtengo_tabla_adicionada()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub bt_confirma_art_Click(sender As Object, e As EventArgs) Handles bt_confirma_art.Click
        If valida_art() = True Then
            bt_confirma_art.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub
    Function valida_art() As Boolean
        If COD_ARTICU <> "" And DESCRIPCIO <> "" Then
            valida_art = True
        Else
            valida_art = False
        End If
    End Function
    Function obtengo_tabla_adicionada()
        Dim Cmd As SqlClient.SqlCommand
        Dim Adp As SqlClient.SqlDataAdapter
        Dim ds As New DataTable
        Dim sbQuery As String

        ds.Clear()
        Conectar()
        Try

            sbQuery = "select STA11.COD_ARTICU,DESCRIPCIO,COD_MEDIDA,PRECIO from sta11 " & _
                      "LEFT JOIN MEDIDA ON STA11.ID_MEDIDA_STOCK = MEDIDA.ID_MEDIDA " & _
                      "LEFT JOIN GVA17 ON STA11.COD_ARTICU = GVA17.COD_ARTICU"

            Cmd = New SqlClient.SqlCommand(sbQuery, cn)
            Adp = New SqlClient.SqlDataAdapter(Cmd)
            Adp.Fill(ds)
            data_Articulos = ds
            If data_Articulos.Rows.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function

End Class